package com.usaas.search.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usaas.common.core.domain.AjaxResult;
import com.usaas.search.dto.PlatGameInfoDto;
import com.usaas.search.mapper.PlatGameInfoMapper;
import com.usaas.search.pojo.PlatGameInfoPojo;
import com.usaas.search.service.IPlatGameInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 游戏平台Service业务层处理
 *
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatGameInfoServiceImpl extends ServiceImpl<PlatGameInfoMapper, PlatGameInfoPojo> implements IPlatGameInfoService {

    @Resource
    private PlatGameInfoMapper platGameInfoMapper;

    /**
     * 查询游戏平台
     *
     * @param id 游戏平台主键
     * @return 游戏平台
     */
    @Override
    public PlatGameInfoDto queryById(Long id) {
        PlatGameInfoPojo pojo = this.getById(id);
        if (pojo != null) {
            PlatGameInfoDto dto = BeanUtil.copyProperties(pojo, PlatGameInfoDto.class);
            List<PlatGameInfoDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询游戏平台列表
     *
     * @param platGameInfoDto 游戏平台
     * @return 游戏平台
     */
    @Override
    public List<PlatGameInfoDto> queryList(PlatGameInfoDto platGameInfoDto) {
        LambdaQueryWrapper<PlatGameInfoPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = this.buildWrapper(queryWrapper, platGameInfoDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatGameInfoPojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatGameInfoDto> dtoList = BeanUtil.copyToList(list, PlatGameInfoDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatGameInfoDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return;
        }
    }

    /**
     * 新增游戏平台
     *
     * @param platGameInfoDto 游戏平台
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatGameInfoDto platGameInfoDto) {

        PlatGameInfoPojo platGameInfoPojo = BeanUtil.copyProperties(platGameInfoDto, PlatGameInfoPojo.class);
        return this.save(platGameInfoPojo);
    }

    /**
     * 修改游戏平台
     *
     * @param platGameInfoDto 游戏平台
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatGameInfoDto platGameInfoDto) {
        PlatGameInfoPojo platGameInfoPojo = BeanUtil.copyProperties(platGameInfoDto, PlatGameInfoPojo.class);
        return this.updateById(platGameInfoPojo);
    }

    /**
     * 批量删除游戏平台
     *
     * @param ids 需要删除的游戏平台主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }


    /**
     * 分页查询${subTable.functionName}信息
     *
     * @param platGameInfoDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatGameInfoDto platGameInfoDto) {
        Page<PlatGameInfoPojo> page = new Page<>(platGameInfoDto.getPageNum(), platGameInfoDto.getPageSize());
        LambdaQueryWrapper<PlatGameInfoPojo> wrapper = new LambdaQueryWrapper<PlatGameInfoPojo>();
        //此处可做前置判断
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        wrapper = buildWrapper(wrapper, platGameInfoDto);
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        Page<PlatGameInfoPojo> pageResult = this.page(page, wrapper);
        Page<PlatGameInfoDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatGameInfoPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatGameInfoDto> dtos = BeanUtil.copyToList(records, PlatGameInfoDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }

    @Override
    public Page<PlatGameInfoDto> queryPage2(PlatGameInfoDto platGameInfoDto) {
        Page<PlatGameInfoPojo> page = new Page<>(platGameInfoDto.getPageNum(), platGameInfoDto.getPageSize());
        LambdaQueryWrapper<PlatGameInfoPojo> wrapper = new LambdaQueryWrapper<PlatGameInfoPojo>();

        Page<PlatGameInfoDto> finalResult = BeanUtil.copyProperties(page, Page.class);

        //此处可做前置判断
        if (wrapper == null) {
            return finalResult;
        }
        wrapper = buildWrapper(wrapper, platGameInfoDto);
        if (wrapper == null) {
            return finalResult;
        }
        Page<PlatGameInfoPojo> pageResult = this.page(page, wrapper);
        finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatGameInfoPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatGameInfoDto> dtos = BeanUtil.copyToList(records, PlatGameInfoDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return finalResult;
    }

    private LambdaQueryWrapper<PlatGameInfoPojo> buildWrapper(LambdaQueryWrapper<PlatGameInfoPojo> wrapper, PlatGameInfoDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
        Long id = param.getId();
        if (id != null) {
            wrapper.eq(PlatGameInfoPojo::getId, id);
        }
        Long gameCategoryId = param.getGameCategoryId();
        if (gameCategoryId != null) {
            wrapper.eq(PlatGameInfoPojo::getGameCategoryId, gameCategoryId);
        }
        String gameName = param.getGameName();
        if (StrUtil.isNotBlank(gameName)) {
            wrapper.like(PlatGameInfoPojo::getGameName, gameName);
        }
        String gameIcon = param.getGameIcon();
        if (StrUtil.isNotBlank(gameIcon)) {
            wrapper.eq(PlatGameInfoPojo::getGameIcon, gameIcon);
        }
        String gameDesc = param.getGameDesc();
        if (StrUtil.isNotBlank(gameDesc)) {
            wrapper.like(PlatGameInfoPojo::getGameDesc, gameDesc);
        }
        String gameImages = param.getGameImages();
        if (StrUtil.isNotBlank(gameImages)) {
            wrapper.eq(PlatGameInfoPojo::getGameImages, gameImages);
        }
        String gameDownloadWay = param.getGameDownloadWay();
        if (StrUtil.isNotBlank(gameDownloadWay)) {
            wrapper.eq(PlatGameInfoPojo::getGameDownloadWay, gameDownloadWay);
        }
        String gameDescOther = param.getGameDescOther();
        if (StrUtil.isNotBlank(gameDescOther)) {
            wrapper.eq(PlatGameInfoPojo::getGameDescOther, gameDescOther);
        }
        String createBy = param.getCreateBy();
        if (StrUtil.isNotBlank(createBy)) {
            wrapper.eq(PlatGameInfoPojo::getCreateBy, createBy);
        }
        Date createTime = param.getCreateTime();
        if (createTime != null) {
            wrapper.eq(PlatGameInfoPojo::getCreateTime, createTime);
        }
        String updateBy = param.getUpdateBy();
        if (StrUtil.isNotBlank(updateBy)) {
            wrapper.eq(PlatGameInfoPojo::getUpdateBy, updateBy);
        }
        Date updateTime = param.getUpdateTime();
        if (updateTime != null) {
            wrapper.eq(PlatGameInfoPojo::getUpdateTime, updateTime);
        }
        String remark = param.getRemark();
        if (StrUtil.isNotBlank(remark)) {
            wrapper.eq(PlatGameInfoPojo::getRemark, remark);
        }
        Long delFlag = param.getDelFlag();
        if (delFlag != null) {
            wrapper.eq(PlatGameInfoPojo::getDelFlag, delFlag);
        }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
